/*
 * Created by Jaren at 2021/10/29 14:16
 */
import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/base/statistics.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class ScooterTotalStatistics extends AbsTotalStatistics {
  Uint32 mileage = Uint32.zero;
  Uint32 energy = Uint32.zero;
  Uint32 powerOnTime = Uint32.zero;

  ScooterTotalStatistics(ProductModel model, VersionWrapper version) : super(model, version);

  @override
  void initCompact() {
    compact = DefaultTotalStatisticsCompact();
  }
}

/// Default protocol version compact
class DefaultTotalStatisticsCompact extends Compact<ScooterTotalStatistics> {
  @override
  void fromBytes(ScooterTotalStatistics output, Uint8ClampedList bytes, int offset) {
    output.mileage = ByteUtil.bytes2Uint32Le(bytes, offset);
    offset += 4;
    output.energy = ByteUtil.bytes2Uint32Le(bytes, offset);
    offset += 4;
    output.powerOnTime = ByteUtil.bytes2Uint32Le(bytes, offset);
  }

  @override
  int length() {
    return 12;
  }
}

class ScooterStatistics extends AbsStatistics {
  Int16 maxMosTemp = Int16.zero;
  Int16 maxMotorTemp = Int16.zero;

  Uint16 maxSpeed = Uint16.zero;
  Uint32 mileage = Uint32.zero;
  Uint32 rideTime = Uint32.zero;
  Uint32 powerOnTime = Uint32.zero;
  Uint32 energy = Uint32.zero;

  ScooterStatistics(ProductModel model, VersionWrapper version) : super(model, version);

  @override
  int statisticsOffset() {
    return 0;
  }

  @override
  void initCompact() {
    compact = DefaultStatisticsCompact();
  }

  @override
  String toString() {
    return 'ScooterStatistics{maxMosTemp: $maxMosTemp, maxMotorTemp: $maxMotorTemp, maxSpeed: $maxSpeed, mileage: $mileage, rideTime: $rideTime, powerOnTime: $powerOnTime, energy: $energy}';
  }
}

/// Default protocol version compact
class DefaultStatisticsCompact extends Compact<ScooterStatistics> {
  @override
  void fromBytes(ScooterStatistics output, Uint8ClampedList bytes, int offset) {
    output.maxMosTemp = ByteUtil.bytes2Int16Le(bytes);
    offset++;
    output.maxMotorTemp = ByteUtil.bytes2Int16Le(bytes);
    offset++;
    output.maxSpeed = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.mileage = ByteUtil.bytes2Uint32Le(bytes, offset);
    offset += 4;
    output.rideTime = ByteUtil.bytes2Uint32Le(bytes, offset);
    offset += 4;
    output.powerOnTime = ByteUtil.bytes2Uint32Le(bytes, offset);
    offset += 4;
    output.energy = ByteUtil.bytes2Uint32Le(bytes, offset);
  }

  @override
  int length() {
    return 20;
  }
}
